package drds.global_transaction.api.rpc.rpc_client.server_addresss_look_up_service;

import drds.common.$;
import drds.global_transaction.common.exception.FrameworkException;
import drds.global_transaction.config.Configuration;
import drds.global_transaction.config.ConfigurationFactory;

import static drds.global_transaction.api.rpc.rpc_client.server_addresss_look_up_service.ConfigurationKeys.server_node_split_char;
import static drds.global_transaction.common.exception.FrameworkErrorCode.InvalidConfiguration;

public class GlobalTransactionServiceGroupAddressManagerStaticConfigImpl implements GlobalTransactionServiceGroupAddressManager
{

	private Configuration configuration = ConfigurationFactory.getInstance();
	private String[] addresses = null;

	@Override
	public String[] lookup(String globalTransactionServiceGroup)
	{
		if (addresses != null)
		{
			return addresses;
		}
		//
		String group = configuration.getConfig(ConfigurationKeys.service_group_mapping_prefix + globalTransactionServiceGroup);
		if (group == null)
		{
			throw new FrameworkException(InvalidConfiguration);
		}
		String groupDataId = ConfigurationKeys.service_prefix + group + ConfigurationKeys.grouplist_postfix;
		String serverListConfig = configuration.getConfig(groupDataId);
		if ($.isNullOrEmpty(serverListConfig))
		{
			throw new FrameworkException(InvalidConfiguration);
		}
		addresses = serverListConfig.split(server_node_split_char);
		if (addresses == null || addresses.length == 0)
		{
			throw new FrameworkException(InvalidConfiguration);
		}
		return addresses;

	}

	@Override
	public void register(String globalTransactionServiceGroup, String address)
	{
		throw new UnsupportedOperationException("Dynamic registry");

	}

	@Override
	public void unregister(String globalTransactionServiceGroup, String address)
	{
		throw new UnsupportedOperationException("Dynamic un-registry");

	}

	@Override
	public void watch(String globalTransactionServiceGroup, AddressWatcher addressWatcher)
	{
		throw new UnsupportedOperationException("Watch");

	}
}
